package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.entity.AdSensitive;
import com.usian.admin.mapper.AdSensitiveMapper;
import com.usian.admin.service.IAdSensitiveService;
import com.usian.common.dtos.PageResult;
import com.usian.common.enums.AppHttpCodeEnum;
import com.usian.common.exception.LeadException;
import com.usian.model.admin.dtos.SensitiveDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 */
@Service
public class  AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements IAdSensitiveService {

    @Override
    public PageResult<AdSensitive> findByPage(SensitiveDto dto) {
        if(dto==null){
            throw new LeadException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //1、构建分页查询条件
        Page<AdSensitive> page = new Page<>(dto.getPage(),dto.getSize());
        //2、构建敏感词名称模糊查询
        QueryWrapper<AdSensitive> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(dto.getSensitives())){
            queryWrapper.lambda().like(AdSensitive::getSensitives,dto.getSensitives());
        }
        //3、执行查询
        IPage<AdSensitive> iPage = this.page(page, queryWrapper);
        //4、封装查询结果
        if(CollectionUtils.isEmpty(iPage.getRecords())){
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST,"敏感词数据不存在");
        }

        return new PageResult<>(dto.getPage(),dto.getSize(),iPage.getTotal(),iPage.getRecords());
    }

    @Override
    public List<String> findAllSensitive() {
        List<AdSensitive> sensitiveList = this.list();
        if(!CollectionUtils.isEmpty(sensitiveList)){
            List<String> list = sensitiveList.stream().map(AdSensitive::getSensitives).collect(Collectors.toList());
            return list;
        }
        return null;
    }
}
